package org.example.fancystudy.cas;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author chaigq
 * @version 1.0
 * @date 2023/2/17 15:41
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
class Book {
    private int id;
    private String bookName;
}
public class AtomicStampedDemo {
    public static void main(String[] args) {
        Book book1 = new Book(1,"javaBook");
        AtomicStampedReference<Book> atomicStampedReference = new AtomicStampedReference<Book>(book1, 1);
        System.out.println(atomicStampedReference.getReference() + "\t" + atomicStampedReference.getStamp());

        Book book2 = new Book(2,"mysqlBook");
        Boolean bool;
        bool = atomicStampedReference.compareAndSet(book1, book2, atomicStampedReference.getStamp(), atomicStampedReference.getStamp()+1);
        System.out.println(bool + "\t" + atomicStampedReference.getReference() + "\t" + atomicStampedReference.getStamp());

        bool = atomicStampedReference.compareAndSet(book2, book1, atomicStampedReference.getStamp(), atomicStampedReference.getStamp()+1);
        System.out.println(bool + "\t" + atomicStampedReference.getReference() + "\t" + atomicStampedReference.getStamp());
    }
}
